package com.houger.ddd.redpacket.infra.mysql;

import com.houger.ddd.redpacket.infra.entity.RedPacket;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author Houger.
 * @date 2024年04月19日 14:20.
 */
@Repository
public interface RedPacketRepository extends JpaRepository<RedPacket,Long>, JpaSpecificationExecutor<RedPacket> {

    RedPacket findByRedPacketId(String redPacketId);

    @Modifying(clearAutomatically = true)
    @Transactional(rollbackFor = Exception.class)
    @Query(value = "update red_packet set residual_amount=?3 ,residual_count=?4,burnout_time=?5 WHERE id = ?1 and residual_amount=?2",
        nativeQuery=true)
    int updateResidualAndBurnoutTime(Long id, String from, String residualAmount, String residualCount, String burnoutTime);
    @Modifying(clearAutomatically = true)
    @Transactional(rollbackFor = Exception.class)
    @Query(value = "update red_packet set residual_amount=?3 ,residual_count=?4 WHERE id = ?1 and residual_amount=?2",
        nativeQuery=true)
    int updateResidual(Long id, String from, String residualAmount, String residualCount);
}
